import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tiktokadult/hj_page/cartoon_business/audio_player_content.dart';
import 'package:tiktokadult/hj_page/cartoon_business/widget/acg_source_manager.dart';
import 'package:tiktokadult/hj_utils/global.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/main.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/widget/follow_button.dart';

import 'cartoon_recommend_page.dart';
import 'voice_novel_read_logic.dart';

//有声小说播放页面
class VoiceNovelReadPage extends StatefulWidget {
  const VoiceNovelReadPage({super.key});

  @override
  State<VoiceNovelReadPage> createState() => _VoiceNovelReadPageState();
}

class _VoiceNovelReadPageState extends State<VoiceNovelReadPage> with RouteAware {
  late String tag;
  late VoiceNovelReadLogic logic;

  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context)!);
  }

  @override
  void initState() {
    super.initState();
    tag = DateTime.now().millisecondsSinceEpoch.toString();
    logic = Get.put(VoiceNovelReadLogic(), tag: tag);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<VoiceNovelReadLogic>(
      tag: tag,
      builder: (_) => ChangeNotifierProvider<ACGSourceManager>.value(
        value: _.sourceManager,
        child: Scaffold(
          appBar: AppBar(
            leading: InkWell(
              child: Icon(
                Icons.arrow_back_ios,
                size: 24,
                color: Colors.white,
              ),
              onTap: () => Get.back(),
            ),
            centerTitle: false,
            titleSpacing: -10,
            title: Text(
              _.title ?? '',
              style: TextStyle(color: Colors.white.withOpacity(.9), fontSize: 18.sp, fontWeight: FontWeight.w600),
            ),
            actions: [
              Padding(
                padding: EdgeInsets.only(right: 16),
                child: Consumer<ACGSourceManager>(
                  builder: (context, mananger, child) {
                    bool col = mananger.mediaInfo?.mediaStatus?.hasCollected ?? false;
                    return FollowButton(
                      mediaId: mananger.mediaInfo?.id ?? '',
                      isFollow: col,
                      followType: FollowEnum.noval,
                      successsAction: (isSuccess) {
                        context.read<ACGSourceManager>().updateLikeState(isSuccess);
                        mananger.updateLikeState(isSuccess);
                        if (isSuccess) {
                          showToast('收藏成功');
                        }
                      },
                    );
                  },
                ),
              )
            ],
          ),
          body: () {
            if (_.isFirstLoad) return LoadingCenterWidget();
            if (_.sourceManager.allEpisodes.isEmpty) return CErrorWidget();
            return ExtendedNestedScrollView(
                headerSliverBuilder: (context, innerBoxIsScrolled) {
                  return [
                    SliverToBoxAdapter(
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 16),
                        child: AudioPlayerContent(
                          _.sourceManager,
                          index: _.index,
                          manager: _.audioManager,
                        ),
                      ),
                    ),
                    0.5.sliverLine,
                    18.sliverSizeBoxH,
                  ];
                },
                body: CartoonRecommendPage(
                  mediaId: _.sourceManager.mediaInfo?.id ?? '',
                  padding: EdgeInsets.only(top: 10),
                  darkStyle: true,
                  tapAction: () => logic.audioManager.pause(),
                ));
          }(),
        ),
      ),
    );
  }

  _line() {
    return SliverToBoxAdapter(
      child: Container(
        height: 0.5,
        margin: EdgeInsets.symmetric(horizontal: 16),
        color: Colors.white.withOpacity(.1),
      ),
    );
  }
}
